package cn.com.controler.StateView;

import cn.com.controler.Util.JDBCUtil;
import cn.com.entity.StateView;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StateViewDAO {

    //删除
    public int delbyid(int id){
        Connection conn = null;
        PreparedStatement statement = null;
        String str = "delete from state where StateId = ?";
        conn = JDBCUtil.getConnection();
        try {
            statement = conn.prepareStatement(str);
            statement.setInt(1,id);
            return statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(conn,statement,null);
        }
        return 0;
    }
    //查询所有
    public StateView selectallState(){
        Connection conn = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        String str = "select *from S_state";
        conn = JDBCUtil.getConnection();
        try {
            statement = conn.prepareStatement(str);
            resultSet = statement.executeQuery();
            if(resultSet.next()){
                StateView stateView = new StateView();
                stateView.setStateId(resultSet.getInt(1));
                stateView.setStateContent(resultSet.getString(2));
                stateView.setStateTime(resultSet.getTimestamp(3));
                stateView.setAdminUsername(resultSet.getString(4));
                return stateView;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(conn,statement,resultSet);
        }
        return null;
    }
}
